import tkinter.ttk as ttk
from abc import abstractmethod

from links.abstract_link import AbstractLink


class AbstractUiLink(ttk.Frame):
    def __init__(self, parent):
        super().__init__(parent)
        self.__io = None

    @abstractmethod
    def create_io(self) -> AbstractLink:
        pass

    @abstractmethod
    def save(self) -> dict:
        pass

    @abstractmethod
    def load(self, parameters: dict):
        pass

    def open_io(self):
        if not self.__io:
            self.__io = self.create_io()
        print("Opening IO")
        self.__io.load(self.save())
        self.__io.daemon = True
        self.__io.start()

    def close_io(self):
        if self.__io:
            print("Closing IO")
            self.__io.set_invoke_terminal(True)
            self.__io.join()
            print("IO closed")
            self.__io = None

    def output_queue(self):
        if self.__io:
            return self.__io.output_queue
        else:
            return None

    def input_queue(self):
        if self.__io:
            return self.__io.input_queue
        else:
            return None
